<template>
	<view class="bef7 h100vh f30 pb130">
		<view class="p3 f-s-c bf mb30 mt20">
			<view class="wei">已选：{{radiovalue1.length}}个礼包</view>
			<view class="">合计：¥ {{price}}</view>
		</view>
		<view class="p3 bf mb30">
			<u-checkbox-group v-model="radiovalue1" placement="column" @change="groupChange">
				<view v-for="(item, index) in radiolist1" :key="index" class="mb30 b-b">
					<u-checkbox :customStyle="{marginBottom: '8px'}" :label="item.name" :name="item.id"></u-checkbox>
					<view class="ml30 mb20">
						<view style="color: #FA8C17;" class="mb10">¥{{item.amount}}</view>
						<img @click="previewImg($config.oss_doman+item.logo)" :src="$config.oss_doman+item.logo"
							style="width: 200rpx;height: 200rpx;">
					</view>
				</view>
			</u-checkbox-group>
		</view>
		<view class="foot-fixed-area bf p30 t-c w100" style="border-top: 1px solid #eee;">
			<view class="bs10 f-c cf p20 m0a" style="background: #0F56B9;width: 80%;" @click="goNext">
				下一步
			</view>
		</view>
	</view>
</template>

<script>
	const $index = require('../../request/api/v1/index');
	export default {
		data() {
			return {
				htmlContent: '',
				// 基本案列数据
				radiolist1: [],
				// u-checkbox-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				radiovalue1: [],
				price: 0,
				selectPackList: []
			};
		},
		onLoad(e) {
			this.getFlow(e.contestId, e.packIds)
		},
		methods: {
			previewImg(url) {
				uni.previewImage({
					current: url,
					urls: [url]
				});
			},
			getFlow(contestId, packIds) {
				$index.giftList({
					contestId
				}).then(res => {
					this.radiolist1 = res.rows;
					// this.radiovalue1 = res.rows[0].id
					if (packIds) {
						let arr = packIds.split(',').map(item => Number(item))
						console.log('arrarr', arr)
						this.radiovalue1 = arr
						this.selectPackList = this.radiolist1.filter(ss => arr.includes(ss.id))
						this.price = 0
						this.selectPackList.forEach(ss => {
							this.price += ss.amount
						})

						// 获取当前页面栈
						const pages = getCurrentPages();
						// 获取上一页实例
						const prevPage = pages[pages.length - 2];
						const currentValue = Number(prevPage.$vm.manInfo.amount) || 0;
						prevPage.setData({
							// selectPackObjList: this.selectPackList,
							// packPrice: this.price,
							'manInfo.amount': currentValue - this.price,
							// 'manInfo.packId': this.radiovalue1.join(',')
						});
						console.log('this.selectPackList', this.selectPackList)
					}
				});
			},
			groupChange(e) {
				console.log('e', e);
				this.price = 0
				this.selectPackList = []
				e.forEach((item, index) => {
					this.radiolist1.forEach((ss, ssindex) => {
						if (item == ss.id) {
							this.price += ss.amount
							this.selectPackList.push(ss)
						}
					})
				})
				// this.price = this.radiolist1.find(item => item.id == e).amount
				// this.selectPackList = this.radiolist1.find(item => item.id == e)
			},
			goNext() {
				if (this.radiovalue1.length == 0) {
					this.$u.toast('请先选择一个礼包')
					return
				}
				// 获取当前页面栈
				const pages = getCurrentPages();
				// 获取上一页实例
				const prevPage = pages[pages.length - 2];

				// 直接修改上一页的数据
				if (prevPage) {
					// 获取上一页的当前值
					const currentValue = Number(prevPage.$vm.manInfo.amount) || 0;
					prevPage.setData({
						selectPackObjList: this.selectPackList,
						packPrice: this.price,
						'manInfo.amount': currentValue + this.price,
						'manInfo.packId': this.radiovalue1.join(',')
					});
				}
				// 返回上一页
				uni.navigateBack();
				//
				// 返回上一页并更新数据
				// uni.emit('changeForm', {
				// 	price: this.price,
				// 	packId: this.radiovalue1
				// })
				// uni.navigateBack({
				// 	delta: 1,
				// })
			}
		}
	};
</script>